import site
site.addsitedir('./src')
from tests_generator import *
class test_test_generator(unittest.TestCase):
    def assert_kiss(self, expected, source):
        global kiss
        return self.assertEqual(*(re.sub(r'^<p>|</p>$|\s+', '', s) for s in (expected, kiss(source).strip())))
    def test_parse_range(self):
        self.assertTrue(0 in parse_range(0, 100))
        self.assertEquals(parse_range(1,5),{1,2,3,4})
        self.assertEquals(parse_range(10,0),set())
        self.assertFalse(196 in parse_range(1,196))
        self.assertTrue(parse_range(-5,-1),None)
    def test_get_arr_by_arr_indexes(self):
        self.assertEquals(get_arr_by_arr_indexes([1,2,3,4],[0,3]) , [1])
        self.assertEquals(get_arr_by_arr_indexes([1,2,3,4],[]) , [])
        self.assertTrue(99 in get_arr_by_arr_indexes(range(1,100),[98,-1]))
        self.assertRaises(IndexError,get_arr_by_arr_indexes,[],[1,2,3])
        arr = [1,2,3,4,5]
        for i in range(6,500):
            arr.append(i)
            self.assertEquals(get_arr_by_arr_indexes(arr,[i-4,i-3,i-2,-1]),[arr[i-4],arr[i-3],arr[i-2]])
    def test_arr_sum_is_between(self):
        self.assertTrue(arr_sum_is_between([],0,0))
        self.assertFalse(arr_sum_is_between([],1,2))
        self.assertTrue(arr_sum_is_between([1,2,3,4,5],10,15))
        self.assertFalse(arr_sum_is_between([1,2,3,4,5],15,10))
        arr = [1]
        for i in range(2,100):
            arr.append(i)
            arr_len = len(arr)
            self.assertTrue(arr_sum_is_between(arr, (arr_len*(arr_len+1))/2 - 1, (arr_len*(arr_len+1))/2 + 1)) 
    def test_get_satisfing_combination(self):
        questions = []
        for i in range(0,100):
            questions.append(question("Question Head" + str(i), ['1','2',str(i)], i % 5, i % 5,' ',1 ))
        for i in range(1,10):
            for j in range(i+10,20):
                arr = (get_satisfing_combination(questions, 3 + i%5, 2 + i%3, j))
                arr_sum = 0
                self.assertTrue(len(arr) == 3 + i%5)
                for each in arr:
                    arr_sum += each.difficult_level
                self.assertTrue(arr_sum >= 2 + i%3 and arr_sum <= j)
        self.assertEquals(get_satisfing_combination(questions, 1, 999 + i%3, 9999), None)
        self.assertEquals(get_satisfing_combination(questions, 1, 1, 0), None)
    def test_read_txt_file(self):
        self.assertEquals(read_txt_file(''),'')
    def test_parse_txt_data(self):
        test_data = """Heading\n3\n2\n42\n40\n39\n\n"""
        self.assertEquals(parse_txt_data(test_data,'')[0],question('Heading',['42', '40', '39'],2,3,' ',1))
        self.assertEquals(len(parse_txt_data(test_data * 5 + '  \n\n', '')),5)
        self.assertEquals(parse_txt_data('',''),[])
        self.assertEquals(parse_txt_data('bullshits',''),[])
    def test_read_and_parse_txt_data(self):
        self.assertEquals(read_and_parse_txt_data('.fakeTestFile LoadingNothing'),[])
    def test_read_and_parse_xml_data(self):
        self.assertEquals(read_and_parse_xml_data('.fakeTestFile LoadingNothing'),[])
    def test_extract_filter_indexes(self):
        self.assertTrue(1 in extract_filter_indexes("1,2,5-8"))
        self.assertFalse(8 in extract_filter_indexes("1,2,5-8"))
        self.assertEquals(extract_filter_indexes("1-5,7-10"),{1,2,3,4,7,8,9})
        self.assertEquals(extract_filter_indexes("5-10"),extract_filter_indexes("5,6,7,8,9"))
        self.assertEquals(extract_filter_indexes(''),set())
        self.assertEquals(extract_filter_indexes('-1,-2'),set())
        self.assertEquals(extract_filter_indexes('bullshits'),set())
        for i in range(1,151):
            self.assertTrue(i in extract_filter_indexes('1-50,50-100,100-101,101,102,103-149,149,150,150,150'))
    #some unit tests borrowed from fmi.py-bg.net
    def test_kiss(self):
        self.assert_kiss('<p>Баба</p><p>Дядо</p>', ('Баба\n\nДядо'))
        self.assert_kiss('<p>Баба<br />и куче</p><p>Дядо<br />и котка</p>', ('Баба\nи куче\n\nДядо\nи котка'))
        self.assert_kiss('<ol type=a><li>Внуче</li><li>Баба</li></ol><p>* Дядо</p>', ('*Внуче\n*Баба\n* Дядо'))
        self.assert_kiss('<ol type=a><li>Баба</li><li>Дядо</li><li>Пуффпафф</li></ol>', ('* Баба\n* Дядо\n* Пуффпафф\n'))
        self.assert_kiss('<p>Ето:</p><ol type=a><li>Баба</li><li>Дядо</li><li>Внуче</li></ol><p>Ньето</p>', ('Ето:\n* Баба\n* Дядо\n* Внуче\nНьето'))
        global question_number
        question_number = 0
        self.assert_kiss('<h3> Плъп Фшикън</h3>', ('=== Плъп Фшикън ==='))
        self.assert_kiss('\n\n\n\n\n\n users loves enter','users loves enter')
    def test_save_html(self):
        self.assertEquals(save_html('<p>±!@£$%^&*(_∑Ето:</p><ol type=a><li>One</li><li>Two</li><li>Three</li></ol><p>Four</p>', 'unitTest'), None)
    def test_read_txt_file(self):
        self.assertEquals(read_txt_file('unitTest.html'),'<p>±!@£$%^&*(_∑Ето:</p><ol type=a><li>One</li><li>Two</li><li>Three</li></ol><p>Four</p>')
    def test_get_needed_questions_indexes(self):
        self.assertEquals(get_needed_questions_indexes(''),{})
if __name__ == '__main__':
    unittest.main()
